class Solution:
    def smallestSubsequence(self, s: str) -> str:
        last_pos = {}
        for i, c in enumerate(s):
            last_pos[c] = i
        char_set = set()

        result = []
        for i, c in enumerate(s):
            if len(result) == 0:
                result.append(c)
                char_set.add(c)
                continue

            if c in char_set:
                continue

            while len(result) != 0 and c < result[-1] and last_pos[result[-1]] > i:
                char_set.remove(result[-1])
                result.pop()
            result.append(c)
            char_set.add(c)

        return ''.join(result)


print(Solution().smallestSubsequence('15432133452'))

